<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>编辑商品</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-alert
        title="编辑商品信息"
        type="info"
        center
        show-icon
        :closable="false"
      >
      </el-alert>
      <el-form
        :model="editGoodsForm"
        :rules="editGoodsFormRules"
        ref="editGoodsFormRef"
        label-width="100px"
        label-position="top"
      >
        <!-- tab页签区域 -->
        <el-tabs
          :before-leave="beforeTabsLeave"
          v-model="activeIndex"
          @tab-click="tabClick"
          :tab-position="'left'"
        >
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="editGoodsForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input
                type="number"
                v-model="editGoodsForm.goods_price"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input
                type="number"
                v-model="editGoodsForm.goods_weight"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input
                type="number"
                v-model="editGoodsForm.goods_number"
              ></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="1">
            <el-upload
              :headers="headersObj"
              :action="uploadURL"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="onPicUploadSuccess"
              :file-list="fileList"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="2">
            <quill-editor
              v-model="editGoodsForm.goods_introduce"
            ></quill-editor>
            <el-button type="primary" class="editBtn" @click="edit"
              >添加商品</el-button
            >
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
    <!-- 图片预览对话框 -->
    <el-dialog title="图片预览" :visible.sync="previewImgVisible" width="50%">
      <img :src="previewPath" alt="" class="previewImg" />
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品ID
      goods_id: '',
      // 商品所有信息
      goods_info: {},
      // tab页签当前选中的index
      activeIndex: '0',
      // 表单信息
      editGoodsForm: {},
      // 表单验证规则对象
      editGoodsFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ],
        goods_number: [
          { required: true, message: '请输入商品数量', trigger: 'blur' }
        ],
        goods_cat: [
          { required: true, message: '请选择商品分类', trigger: 'blur' }
        ]
      },
      // 图片上传组件的文件内容
      fileList: [],
      // 图片上传的API地址
      uploadURL: 'http://127.0.0.1:8888/api/private/v1/upload',
      // 图片上传时用到的headers对象
      headersObj: {
        Authorization: window.sessionStorage.getItem('token')
      },
      // 预览图的地址
      previewPath: '',
      // 预览图对话框的显示
      previewImgVisible: false
    }
  },
  created() {
    this.goods_id = this._self.$route.query.id
    if (!this.goods_id) {
      return this.$message.error('请求参数错误！')
    }
    this.getGoodsInfo()
  },
  methods: {
    // 获取商品信息
    async getGoodsInfo() {
      const { data: res } = await this.$http.get(`goods/${this.goods_id}`)
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.goods_info = res.data
      this.editGoodsForm = res.data
      // 把图片列表拿出来给图片上传组件调用
      this.editGoodsForm.pics.forEach(item => {
        const newPic = {
          name: item.pics_id,
          url: item.pics_big
        }
        this.fileList.push(newPic)
      })
    },
    // tab单击事件
    tabClick() {},
    // tab页签切换之前
    beforeTabsLeave() {},
    // 点击图片预览时
    handlePreview(file) {
      // console.log(file)
      this.previewPath = file.url
      this.previewImgVisible = true
    },
    // 删除图片时
    handleRemove(response, file) {
      this.fileList = file
      // console.log(file)
      console.log(this.fileList)
    },
    // 图片上传成功时
    onPicUploadSuccess(response, file) {
      // console.log(file, response)
      if (response.meta.status !== 200) {
        return this.$message.error('图片上传失败')
      }
      const newPic = {
        name: file.name,
        url: response.data.url
      }
      this.fileList.push(newPic)
      console.log(this.fileList)
    },
    // 点击编辑
    edit() {
      this.$message.info('由于说明文档不全，所以本功能无效！')
      // this.$refs.editGoodsFormRef.validate(async valid => {
      //   if (!valid) {
      //     return this.$message.error('带星号的为必填项！')
      //   }
      //   const subForm = {
      //     goods_name: this.editGoodsForm.goods_name,
      //     goods_price: this.editGoodsForm.goods_price,
      //     goods_number: this.editGoodsForm.goods_number,
      //     goods_weight: this.editGoodsForm.goods_weight,
      //     goods_introduce: this.editGoodsForm.goods_introduce,
      //     attrs: this.editGoodsForm.attrs,
      //     pics: []
      //   }
      //   // 处理图片数组
      //   this.fileList.forEach(item => {
      //     subForm.pics.push({
      //       pic: item.url
      //     })
      //   })
      //   const { data: res } = await this.$http.put(
      //     `goods/${this.editGoodsForm.goods_id}`,
      //     subForm
      //   )
      //   // if (res.meta.status !== 200)
      //   console.log(res)
      //   console.log(subForm)
      // })
      // console.log(this.editGoodsForm)
    }
  }
}
</script>

<style lang="less" scoped>
.el-tabs {
  margin-top: 15px;
}
.previewImg {
  width: 100%;
}
.editBtn {
  margin-top: 15px;
}
</style>
